package com.liusand.activity.service.impl;

import com.alibaba.fastjson.JSON;
import com.liusand.activity.mapper.ActivityMapper;
import com.liusand.activity.service.ActivityService;
import com.liusand.activity.thread.UserThreadLocalUtil;
import com.liusand.common.constans.RedisPrefixCode;
import com.liusand.entity.dto.ActivityInfoDto;
import com.liusand.entity.pojo.ActivityInfo;
import com.liusand.entity.pojo.RedPackageInfo;
import com.liusand.entity.vo.MoneyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.activity.service.impl
 * fileName :ActivityServiceImpl
 * creationTime :2022/12/22 16:42
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addActivity(ActivityInfoDto activityInfoDto) {
        if(activityInfoDto == null
                || activityInfoDto.getAmount() == null
                || activityInfoDto.getMoney() == null
                || activityInfoDto.getEndTime() == null
                || activityInfoDto.getStartTime() == null
                || StringUtils.isEmpty(activityInfoDto.getName())){
            throw new RuntimeException("添加参数异常");
        }
        ActivityInfo activityInfo = new ActivityInfo();
        BeanUtils.copyProperties(activityInfoDto, activityInfo);
        activityInfo.setState(0);
        int insertId = activityMapper.insertActivityInfo(activityInfo);

        List<RedPackageInfo> redPackageInfoList = new ArrayList<>();
        int money = activityInfo.getMoney() / activityInfo.getAmount();
        for(int i = 0; i < activityInfo.getAmount(); i++){
            RedPackageInfo redPackageInfo = new RedPackageInfo();
            redPackageInfo.setActivityId(insertId);
            redPackageInfo.setState(0);
            redPackageInfo.setCreateTime(new Date());
            redPackageInfo.setMoney(money);
            redPackageInfoList.add(redPackageInfo);
        }
        int redPackageInfosResult = activityMapper.insertRedPackageInfos(redPackageInfoList);
        if(redPackageInfosResult != activityInfo.getAmount()){
            throw new RuntimeException("数据库插入异常");
        }
        return true;
    }

    @Override
    public List<ActivityInfo> checkActivity() {
        Date now = new Date();
        //在有效期内
        return activityMapper.selectActivityInValidity(now);
    }

    @Override
    public List<RedPackageInfo> findRedPackageInValidity(List<ActivityInfo> activityInfoList) {
        return activityMapper.selectRedPackageInValidity(activityInfoList);
    }

    @Override
    public boolean modifyActivityInfoState(List<ActivityInfo> activityInfoList, Integer newState) {
        Integer result = activityMapper.updateActivityInfoState(activityInfoList, newState);
        if(result != 1){
            throw new RuntimeException("更新活动状态异常");
        }
        return true;
    }

    @Override
    public MoneyVo getRedPackage() {
        MoneyVo moneyVo = new MoneyVo();
        String s = redisTemplate.opsForList().rightPop(RedisPrefixCode.ACTIVITY_RED_PACKAGE_KEY);
        if(StringUtils.isEmpty(s)){
            moneyVo.setMoney(0);
            return moneyVo;
        }

        String userId = UserThreadLocalUtil.getId();
        RedPackageInfo redPackageInfo = JSON.parseObject(s, RedPackageInfo.class);
        if(redPackageInfo == null){
            throw new RuntimeException("红包解析异常");
        }
        Integer result = activityMapper.updateRedPackageInfoById(redPackageInfo.getId(), userId);
        if(result != 1){
            throw new RuntimeException("红包解析异常");
        }
        moneyVo.setMoney(redPackageInfo.getMoney());
        return moneyVo;
    }
}
